package cn.iocoder.yudao.module.system.controller.admin.zkdatanewrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.zkdatanewrecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.zkdatanewrecord.ZkdataNewRecordDO;
import cn.iocoder.yudao.module.system.service.zkdatanewrecord.ZkdataNewRecordService;

@Tag(name = "管理后台 - 数据项")
@RestController
@RequestMapping("/system/zkdata-new-record")
@Validated
public class ZkdataNewRecordController {

    @Resource
    private ZkdataNewRecordService zkdataNewRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建数据项")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:create')")
    public CommonResult<Long> createZkdataNewRecord(@Valid @RequestBody ZkdataNewRecordSaveReqVO createReqVO) {
        return success(zkdataNewRecordService.createZkdataNewRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据项")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:update')")
    public CommonResult<Boolean> updateZkdataNewRecord(@Valid @RequestBody ZkdataNewRecordSaveReqVO updateReqVO) {
        zkdataNewRecordService.updateZkdataNewRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:delete')")
    public CommonResult<Boolean> deleteZkdataNewRecord(@RequestParam("id") Long id) {
        zkdataNewRecordService.deleteZkdataNewRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:query')")
    public CommonResult<ZkdataNewRecordRespVO> getZkdataNewRecord(@RequestParam("id") Long id) {
        ZkdataNewRecordDO zkdataNewRecord = zkdataNewRecordService.getZkdataNewRecord(id);
        return success(BeanUtils.toBean(zkdataNewRecord, ZkdataNewRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据项分页")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:query')")
    public CommonResult<PageResult<ZkdataNewRecordRespVO>> getZkdataNewRecordPage(@Valid ZkdataNewRecordPageReqVO pageReqVO) {
        PageResult<ZkdataNewRecordDO> pageResult = zkdataNewRecordService.getZkdataNewRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZkdataNewRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据项 Excel")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZkdataNewRecordExcel(@Valid ZkdataNewRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZkdataNewRecordDO> list = zkdataNewRecordService.getZkdataNewRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据项.xls", "数据", ZkdataNewRecordRespVO.class,
                        BeanUtils.toBean(list, ZkdataNewRecordRespVO.class));
    }

}